// src/db.js
import { openDB } from 'idb';

const dbPromise = openDB('file-chunks-db', 1, {
    upgrade(db) {
        if (!db.objectStoreNames.contains('chunks')) {
            db.createObjectStore('chunks', { keyPath: 'id', autoIncrement: true });
        }
    },
});

export const addFile = async (file) => {
    return (await dbPromise).add('chunks', file);
};

export const getFile = async (id) => {
    return (await dbPromise).get('chunks', id);
};

export const getAllFiles = async () => {
    return (await dbPromise).getAll('chunks');
};

export const deleteFile = async (id) => {
    return (await dbPromise).delete('chunks', id);
};

export const readwrite = async () => {
    return (await dbPromise).transaction('chunks','readwrite')
}
export const readonly = async () => {
    return (await dbPromise).transaction('chunks','readonly')
}
